<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>击鼓传花</title>
</head>
<body>
    <script>
        function Queue() {
            this.items = [];

            // 1. enqueue(ele): 向队列尾部添加一个元素
            Queue.prototype.enqueue = function(ele) {
                this.items.push(ele)
            }

            // 2. dequeue()：删除队列头部的一个元素并返回
            Queue.prototype.dequeue = function() {
                return this.items.shift();
            }

            // 3. front(): 查看队列的第一个元素
            Queue.prototype.front = function() {
                return this.items[0]
            }

            // 4. isEmpty()：判断队列是否为空
            Queue.prototype.isEmpty = function() {
                return this.items.length === 0
            }

            // 5. size(): 返回队列元素个数
            Queue.prototype.size = function() {
                return this.items.length;
            }

            // 6. 将队列中的内容，转成字符串形式
            Queue.prototype.toString = function() {

                let result = '';
                for(let i = 0; i < this.items.length; i++) {
                    result += this.items[i] + ' '
                }
                return result;
            }
        }

        function passGame(nameList, num) {
            var queue = new Queue();

            for(let i = 0; i < nameList.length; i++) {
                queue.enqueue(nameList[i])
            }

            while(queue.size() > 1) {
                for(let i = 0; i < num - 1; i++) {
                    queue.enqueue(queue.dequeue())
                }
                queue.dequeue()
            }

            return nameList.indexOf(queue.front())
        }

        persons = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'maqi']
        console.log(passGame(persons, 3));
        
        
    </script>
</body>
</html>